<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class PayController extends Controller
{
    //
    private $_public_key;
    private $_private_key;
    public function __construct()
    {
        $this->_public_key=base_path().'/cert/abm_public_key.pem';
        $this->_private_key=base_path().'/cert/abm_private_key.pem';
    }

    private  function ASCII($param=array())
    {
        if(!empty($param))
        {
            $check=ksort($param);
            if($check)
            {
                $str='';
                foreach ($param as $k=>$v)
                {
                    $str.=$k.'='.$v.'|';
                }
                $strs=rtrim($str,'|');
                return $strs;
            }
        }
        return ['error'=>'20001','data'=>'参数错误'];
    }


    private function getSign($data)
    {
        $key=openssl_pkey_get_private(file_get_contents($this->_private_key));
        openssl_sign($data,$signData,$key,OPENSSL_ALGO_SHA256);
        $sign=base64_encode($signData);
        return $sign;
    }

    private function getSignVerify($data,$sign)
    {
        $sign = base64_decode($sign);
        $key = openssl_pkey_get_public(file_get_contents($this->_public_key));
        $result = openssl_verify($data, $sign, $key, OPENSSL_ALGO_SHA256) === 1;//1成功 0失败 -1错误
        return $result;
    }
    private function getParams()
    {
        $params=[
            'platNo'=>'ABM0001',
            'transId'=>'FXT0001',
            'custName'=>'测试人甲',
            'idType'=>'A',
            'idNo'=>'11111111111',
            'mobileNo'=>'18867834756',
            'openBank'=>'',
            'cardNo'=>'',
            'notifyUrl'=>'',
            'returnUrl'=>'',
//            'signData'=>'',

        ];
        return $params;
    }
    public function getPostParams($data=array())
    {
        if(empty($data))
        {
            return ['error'=>'20001','data'=>'参数错误'];
        }
        $params=$this->ASCII($data);
        $signData=$this->getSign($params);
        $data['signData']=$signData;
       return ['error'=>'0','data'=>json_encode($data)];
    }
    public function getResult(Request $request)
    {
        $param=$this->getParams();
        $result=$this->getPostParams($param);
        $url=$request->input('url');
        $resut=$this->sendPost($url,$result);
        dd($resut);
    }

    public function check(Request $request)
    {
        dd($request);
    }

    public function sendPost($url,$data)
    {
        $postParams=http_build_query($data);
        $option=[
            'http'=>[
                'method'=>'post',
                'header'=>'Content-Type:UTF-8',
                'content'=>$postParams,
                'timeout'=>15 * 60,
            ]
        ];
        $context=stream_context_create($option);
        $result = file_get_contents($url, false, $context);
        return $result;
    }
    public function test(Request $request)
    {
        $id=$request->input('id');
        $name=$request->input('name');

        Log::error('时间：'.date("Y-m-d H:i:s",time()).'id:'.$id.'name:'.$name);
    }


    public function sum($carry,$item)
    {
        $carry+=$item;
        return $carry;
    }
    public function product($carry, $item)
    {
        $carry *= $item;
        return $carry;
    }
    public function test_1()
    {
        $arr=[1,2,3,4,5,6];
        $list=0;
        $result=array_reduce($arr,'sum');
        $result_1=array_reduce($arr,'product');
        Log::error('测试：'.$result."|".$result_1);
    }
}
